<?php

namespace app\api\model;

use think\Model;

class Dynamic extends Model
{

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 朋友圈列表
     * @param null $user_id
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dynamicData($user_id = null)
    {
        $where = [];
        $userWhere = [];
        if ($user_id) {
            $likeWhere['user_id'] = $user_id;
            $where['a.user_id'] = $user_id;
        }
        //获取动态列表
        $data = $this->where($where)
            ->alias('a')
            ->join('user_info b', 'a.user_id = b.user_id')
            ->field("a.id,b.username,b.avatar,b.status,gender,birthday,a.address,a.content,a.images,FROM_UNIXTIME(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime")
            ->order('id desc')
            ->paginate(10);
        foreach ($data as $k => $v) {
            $data[$k]['user_age'] = user_age($v['birthday']);
            $data[$k]['members'] = model('members')->where('user_id', $user_id)->value('status') ?? 0;//会员等级
            $dynamicWhere['dynamic_id'] = $v['id'];
            $data[$k]['like_status'] = $this->name('dynamic_like')->where($userWhere)->where($dynamicWhere)->find() ? 1 : 0;//判断是否已点赞
            //点赞列表
            $like_list = $this->name('dynamic_like')
                ->alias('a')
                ->join('user_info b', 'a.user_id = b.user_id')
                ->where(['dynamic_id' => $v['id']])
                ->field('b.username')
                ->select();
            $like_list_str = '';
            //组装成字符串
            foreach ($like_list as $val) {
                $like_list_str .= $val['username'] . ',';
            }
            if (!empty($like_list_str)) {
                $like_list_str = substr($like_list_str, 0, -1);
            }
            $data[$k]['like_list'] = $like_list_str;
            $data[$k]['like_num'] = count($like_list);//点赞数量
            $where['a.dynamic_id'] = $v['id'];
            //评论列表
            $comment_list = commentList($v['id']);
            $data[$k]['comment_list'] = $comment_list;
            $data[$k]['comment_num'] = count($comment_list);//评论数量
        }
        return $data;
    }


    /**
     *
     * 首页列表
     */

    public function indexList($city, $search = null)
    {
        $where['b.city'] = ['like', $city.'%'];
        if($search){
            $where['b.username'] = ['like', '%'.$search.'%'];
        }
        //获取动态列表
        $data = $this->where($where)
            ->alias('a')
            ->join('user_info b', 'a.user_id = b.user_id')
            ->field("a.id,a.user_id,a.content,a.images,a.address,b.username,b.avatar,b.birthday
            ,b.status as auth_status,b.gender,FROM_UNIXTIME(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime")
            ->order('id desc')
            ->paginate(10);
        foreach ($data as $k=>$v){
            $data[$k]['user_age'] = user_age($v['birthday']);//年龄
            $data[$k]['members'] = model('members')->where('user_id', $v['user_id'])->value('status') ?? 0;//会员等级
        }
        return $data;
    }
}